home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / SpriteWorld 2.2 / SpriteWorld Examples / Shark Attack / Sources & Headers / Level.c < prev    next >
Encoding:
Text File  |  1999-01-08  |  8.1 KB  |  332 lines  |  [TEXT/CWIE]

  1. ///--------------------------------------------------------------------------------------
  2. // Level.c
  3. //
  4. // Created 10/7/96
  5. ///--------------------------------------------------------------------------------------
  6.  
  7.  
  8. #include <SWIncludes.h>
  9. #include <SWGameUtils.h>
  10.  
  11. #include "Level.h"
  12. #include "SWSounds.h"
  13. #include "NewSprite.h"
  14. #include "Shark Attack.h"
  15. #include "SpriteMoveProcs.h"
  16. #include "Special Effects.h"
  17. #include "GlobalVariables.h"
  18.  
  19. #define kGetReadyTime            60        // How many frames to display "Get Ready" message
  20.  
  21. #define kMaxNumFishOnScreen        4        // Max number of fish and sharks that
  22. #define kMaxNumSharksOnScreen    1        // can be on the screen at once.
  23. #define kFishSpeed                3
  24. #define kSharkSpeed                4
  25.  
  26.  
  27.     // Stores whether the last fish was added on the left side of the screen or not.
  28. Boolean        gAddedOnLeftSide;
  29.  
  30.  
  31. ///--------------------------------------------------------------------------------------
  32. //  DoGetReadyMessage
  33. ///--------------------------------------------------------------------------------------
  34.  
  35. void    DoGetReadyMessage( void )
  36. {
  37.     unsigned long    junkTime;
  38.     
  39.     SWSetPortToWorkArea(gSpriteWorldP);
  40.     
  41.     ForeColor(blackColor);
  42.     PaintRect(&gSpriteWorldP->backRect);
  43.     
  44.     ForeColor(redColor);
  45.     DrawTextInWorkArea("\pGet Ready!", 48);
  46.     
  47.     WipeWindow(gSpriteWorldP);
  48.     Delay(90, &junkTime);
  49. }
  50.  
  51.  
  52. ///--------------------------------------------------------------------------------------
  53. //  DoNextLevelMessage
  54. ///--------------------------------------------------------------------------------------
  55.  
  56. void    DoNextLevelMessage( void )
  57. {
  58.     unsigned long    junkTime;
  59.     Str255            levelString;
  60.     
  61.     SWSetPortToWorkArea(gSpriteWorldP);
  62.     
  63.     ForeColor(blackColor);
  64.     PaintRect(&gSpriteWorldP->backRect);
  65.     
  66.     ForeColor(cyanColor);
  67.     DrawTextInWorkArea("\pLevel ", 48);
  68.     NumToString(gCurrentLevel, levelString);
  69.     ForeColor(cyanColor);
  70.     DrawString(levelString);
  71.     ForeColor(blackColor);
  72.     
  73.     WipeWindow(gSpriteWorldP);
  74.     Delay(120, &junkTime);
  75. }
  76.  
  77.  
  78. ///--------------------------------------------------------------------------------------
  79. //  DoGameOver
  80. ///--------------------------------------------------------------------------------------
  81.  
  82. void    DoGameOver( void )
  83. {
  84.     unsigned long    junkTime;
  85.     
  86.     SWSetPortToWorkArea(gSpriteWorldP);
  87.  
  88.     ForeColor(blackColor);
  89.     PaintRect(&gSpriteWorldP->backRect);
  90.     
  91.     ForeColor(blueColor);
  92.     DrawTextInWorkArea("\pGame Over", 64);
  93.     
  94.     WipeWindow(gSpriteWorldP);
  95.     PlaySound(kSharkDeadSnd, 2, kFindEmptyChannel);
  96.     Delay(120, &junkTime);
  97. }
  98.  
  99.  
  100. ///--------------------------------------------------------------------------------------
  101. //  DrawTextInWorkArea
  102. ///--------------------------------------------------------------------------------------
  103.  
  104. void    DrawTextInWorkArea(StringPtr textString, short fontSize)
  105. {
  106.     Rect    textRect;
  107.     
  108.     SWSetPortToWorkArea(gSpriteWorldP);
  109.     
  110.     TextFont(systemFont);
  111.     TextFace(bold);
  112.     TextSize(fontSize);
  113.     
  114.     textRect.top = 0;
  115.     textRect.left = 0;
  116.     textRect.bottom = 0;
  117.     textRect.right = StringWidth(textString);
  118.     CenterRect(&textRect, &gSpriteWorldP->backRect);
  119.     
  120.     MoveTo(textRect.left, textRect.top);
  121.     DrawString(textString);
  122.  
  123.     ForeColor(blackColor);
  124. }
  125.  
  126.  
  127. ///--------------------------------------------------------------------------------------
  128. //  SetUpLevel - prepare the animation
  129. ///--------------------------------------------------------------------------------------
  130.  
  131. void    SetUpLevel( void )
  132. {
  133.     gFishDelay = 0;
  134.     gSharkDelay = kMinNewSharkDelay;
  135.     
  136.     AddSubmarine();
  137. }
  138.  
  139.  
  140. ///--------------------------------------------------------------------------------------
  141. // RestartLevel - called when the submarine dies
  142. ///--------------------------------------------------------------------------------------
  143.  
  144. void    RestartLevel( void )
  145. {
  146.     RemoveAllSprites(gSpriteWorldP);
  147.     
  148.     DoGetReadyMessage();
  149.     
  150.     SetUpLevel();
  151.     SWUpdateSpriteWorld(gSpriteWorldP, false);
  152.     WipeWindow(gSpriteWorldP);
  153. }
  154.  
  155.  
  156. ///--------------------------------------------------------------------------------------
  157. // AdvanceLevel - called when it's time to go on to the next level
  158. ///--------------------------------------------------------------------------------------
  159.  
  160. void    AdvanceLevel( void )
  161. {
  162.     short    numFrames = 90;
  163.     
  164.         // Run animation for a little bit before stopping
  165.     while ( numFrames )
  166.     {
  167.         SWProcessSpriteWorld(gSpriteWorldP);
  168.         SWAnimateSpriteWorld(gSpriteWorldP);
  169.         UpdateKeys();
  170.         
  171.         if (gSpriteWorldP->frameHasOccurred)
  172.             numFrames--;
  173.     }
  174.  
  175.     RemoveAllSprites(gSpriteWorldP);
  176.     
  177.     gCurrentLevel++;
  178.     gNextLevelScore += kAdvanceLevelScore;    // Set the score for the next level
  179.     DoNextLevelMessage();
  180.     
  181.     SetUpLevel();
  182.     SWUpdateSpriteWorld(gSpriteWorldP, false);
  183.     WipeWindow(gSpriteWorldP);
  184. }
  185.             
  186.  
  187. ///--------------------------------------------------------------------------------------
  188. //  ProcessLevel - keep adding fish to the level
  189. ///--------------------------------------------------------------------------------------
  190.  
  191. void    ProcessLevel( void )
  192. {
  193.         // Don't add any fish if it's time to advance to the next level
  194.     if (gScore > gNextLevelScore)
  195.         return;
  196.     
  197.     if (gNumFishOnScreen < kMaxNumFishOnScreen)
  198.     {
  199.         if (gFishDelay > 0)
  200.         {
  201.             gFishDelay--;
  202.         }
  203.         else if (GetRandom(0, kFishRandomness) == 0)
  204.         {
  205.             AddFish();
  206.             gFishDelay = kMinNewFishDelay;
  207.         }
  208.     }
  209.     
  210.     
  211.     if (gNumSharksOnScreen < kMaxNumSharksOnScreen)
  212.     {
  213.         if (gSharkDelay > 0)
  214.         {
  215.             gSharkDelay--;
  216.         }
  217.         else if (GetRandom(0, kSharkRandomness) == 0)
  218.         {
  219.  
  220.             AddShark();
  221.             gSharkDelay = kMinNewSharkDelay;
  222.         }
  223.     }
  224. }
  225.  
  226.  
  227. ///--------------------------------------------------------------------------------------
  228. // AddSubmarine
  229. ///--------------------------------------------------------------------------------------
  230.  
  231. void    AddSubmarine( void )
  232. {
  233.     gSubCloneP = NewSubSprite();
  234.     
  235.     SWSetSpriteLocation(gSubCloneP, ((SubStructPtr)gSubCloneP)->horizPos, 
  236.             ((SubStructPtr)gSubCloneP)->vertPos);
  237. }
  238.  
  239.  
  240. ///--------------------------------------------------------------------------------------
  241. //  AddFish - add a fish to the animation
  242. ///--------------------------------------------------------------------------------------
  243.  
  244. void    AddFish( void )
  245. {
  246.     short        horizLoc, vertLoc;
  247.     short        addToLeftSide;
  248.     short        horizDelta;
  249.     SpritePtr    fishSpriteP;
  250.     
  251.     fishSpriteP = NewFishSprite();
  252.     
  253.         // Make the next fish come from the opposite side of the screen most of the time.
  254.     if (gAddedOnLeftSide)
  255.     {
  256.         if (GetRandom(0,30) == 0)
  257.             addToLeftSide = true;
  258.         else
  259.             addToLeftSide = false;
  260.     }
  261.     else
  262.     {
  263.         if (GetRandom(0,30) == 0)
  264.             addToLeftSide = false;
  265.         else
  266.             addToLeftSide = true;
  267.     }
  268.     
  269.     if (addToLeftSide)
  270.     {
  271.             // Add fish to left size
  272.         horizDelta = kFishSpeed;
  273.         horizLoc = -(fishSpriteP->destFrameRect.right - fishSpriteP->destFrameRect.left);
  274.         SWSetSpriteFrameRange(fishSpriteP, 0, 2);
  275.         SWSetCurrentFrameIndex(fishSpriteP, 0);
  276.         gAddedOnLeftSide = true;
  277.     }
  278.     else
  279.     {
  280.             // Add fish to right side
  281.         horizDelta = -kFishSpeed;
  282.         horizLoc = gSpriteWorldP->backRect.right;
  283.         SWSetSpriteFrameRange(fishSpriteP, 3, 5);
  284.         SWSetCurrentFrameIndex(fishSpriteP, 3);
  285.         gAddedOnLeftSide = false;
  286.     }
  287.     
  288.     vertLoc = GetRandom(0, gSpriteWorldP->backRect.bottom - 
  289.         (fishSpriteP->destFrameRect.bottom - fishSpriteP->destFrameRect.top));
  290.     
  291.     SWSetSpriteMoveDelta(fishSpriteP, horizDelta, 0);
  292.     SWSetSpriteLocation(fishSpriteP, horizLoc, vertLoc);
  293. }
  294.  
  295.  
  296. ///--------------------------------------------------------------------------------------
  297. //  AddShark - add a shark to the animation
  298. ///--------------------------------------------------------------------------------------
  299.  
  300. void    AddShark( void )
  301. {
  302.     short            horizPos, vertPos;
  303.     SpritePtr        sharkSpriteP;
  304.     SharkStructPtr    sharkStructP;
  305.     
  306.     sharkSpriteP = NewSharkSprite();
  307.     sharkStructP = (SharkStructPtr)sharkSpriteP;
  308.     
  309.         // Add shark on right or left side
  310.     if (GetRandom(0,1) == 0)
  311.     {
  312.         sharkStructP->horizDelta = -kSharkSpeed;
  313.         SWSetSpriteFrameRange(sharkSpriteP, 3, 5);
  314.         SWSetCurrentFrameIndex(sharkSpriteP, 3);
  315.         horizPos = gSpriteWorldP->backRect.right;
  316.     }
  317.     else
  318.     {
  319.         sharkStructP->horizDelta = kSharkSpeed;
  320.         SWSetSpriteFrameRange(sharkSpriteP, 0, 2);
  321.         SWSetCurrentFrameIndex(sharkSpriteP, 0);
  322.         horizPos = -(sharkSpriteP->destFrameRect.right - sharkSpriteP->destFrameRect.left);
  323.     }
  324.     
  325.         // Place shark at same vertical location as sub
  326.     vertPos = gSubCloneP->destFrameRect.top + kSubHeight/2 - kSharkHeight/2;
  327.     
  328.     sharkStructP->horizPos = horizPos;
  329.     sharkStructP->vertPos = vertPos;
  330.     SWSetSpriteLocation(sharkSpriteP, horizPos, vertPos);
  331. }
  332.